package com.example.bootdemo.common;

import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@NoArgsConstructor
@AllArgsConstructor
public class RestResult<T> {
    public static final int CODE_OK = 200;
    public static final int CODE_INTERNAL_ERROR = 1001;
    public static final int CODE_EXISTS = 1020;
    public static final int CODE_NOT_FOUND = 1021;
    public static final int CODE_NO_PERM = 403;
    private String message;
    private int code;
    private T data;
    private String status;

    public RestResult(String status, int code, String message) {
        this.status = status;
        this.code = code;
        this.message = message;
    }

    public static <T> RestResult<T> ok() {
        return new RestResult("success", 200, "操作成功");
    }

    public static <T> RestResult<T> ok(T data) {
        return (new RestResult("success", 200, "操作成功")).data(data);
    }

    public static <T> RestResult<T> err() {
        return new RestResult("error", 200, "操作失败");
    }

    public static <T> RestResult<T> err(String message) {
        return new RestResult("error", 200, message);
    }

    public static <T> RestResult<T> existsErr() {
        return new RestResult("error", 1020, "数据已存在");
    }

    public static <T> RestResult<T> permErr() {
        return new RestResult("error", 403, "数据无权限");
    }

    public static <T> RestResult<T> notFoundErr() {
        return new RestResult("error", 1021, "数据不存在");
    }

    public static <T> RestResult<T> sysErr() {
        return new RestResult("error", 1001, "系统内部错误");
    }

    public static <T> RestResult<T> sysErr(T data) {
        return (new RestResult("error", 1001, "系统内部错误")).data(data);
    }

    public static <T> RestResult<T> reqErr() {
        return new RestResult("error", 1004, "不支持的请求方式");
    }

    public RestResult<T> data(T object) {
        this.data = object;
        return this;
    }

    public RestResult<T> msg(String message) {
        this.message = message;
        return this;
    }

    public RestResult<T> msg(String format, Object... messages) {
        this.message = String.format(format, messages);
        return this;
    }

    public RestResult<T> code(int code) {
        this.code = code;
        return this;
    }



    public static class Status {
        public static final String SUCCESS = "success";
        public static final String ERROR = "error";

        public Status() {
        }
    }
}

